package de.pyranja.util;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Properties;
import java.util.Random;
import java.util.ResourceBundle;

public class Utils {
	
	/**
	 * loads settings from a properties file present on the classpath of this class's classloader
	 * @param propfile qualified file name
	 * @return
	 */
	public static Properties loadProps(String propfile) {
		Properties result = null;
		try ( 
				InputStream in = Utils.class.getClassLoader().getResourceAsStream(propfile) 
				) {
			result = new Properties();
			result.load(in);
		} catch (Exception e) {
			throw new IllegalArgumentException("resource ["+ propfile +"] is not available",e);
		}
		return result;
	}
	
	/**
	 * load a value list from a resource bundle delimited by a whitspace
	 * @param bundleName
	 * @param propName
	 * @return
	 */
	public static List<String> loadFromBundle(String bundleName, String propName) {
		return loadFromBundle(bundleName, propName, " ");
	}
	
	/**
	 * load a list of values, delimited by specified delimiter, from a resource bundle
	 * @param bundleName
	 * @param propName
	 * @param delimiter
	 * @return
	 */
	public static List<String> loadFromBundle(String bundleName, String propName, String delimiter) {
		ResourceBundle rb = ResourceBundle.getBundle(bundleName);
		List<String> result = new ArrayList<String>( Arrays.asList( rb.getString(propName).split(delimiter) ) );
		return result;
	}
	
	/**
	 * random element from list a specified random number source
	 * @param fromMe
	 * @param rng
	 * @return
	 */
	public static <T> T randomElem(List<T> fromMe, Random rng) {
		return fromMe.get(rng.nextInt(fromMe.size()));
	}
	
	/**
	 * random element from list
	 * @param fromMe
	 * @return
	 */
	public static <T> T randomElem(List<T> fromMe) {
		return randomElem(fromMe, new Random(System.currentTimeMillis()));
	}
	
	/**
	 * Creates random number with specified length from passed rng
	 * @param rng
	 * @param length
	 * @return
	 */
	public static String randomNumber(Random rng, int length) {
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < length; i++) {
			sb.append(rng.nextInt(10));
		}
		return sb.toString();
	}
	
	/**
	 * Creates random with specified length
	 * @param length
	 * @return
	 */
	public static String randomNumber(int length) {
		return randomNumber(new Random(System.currentTimeMillis()), length);
	}
	
	/**
	 * random int in specified range 
	 * @param min
	 * @param max
	 * @return
	 */
	public static int rangedRandomInt(int min, int max) {
		return rangedRandomInt(min, max, new Random(System.currentTimeMillis()));
	}
	
	/**
	 * random int in specified range from passed rng
	 * @param min
	 * @param max
	 * @param rng
	 * @return
	 */
	public static int rangedRandomInt(int min, int max, Random rng) {
		return min + rng.nextInt(max - min);
	}
}